package com.moonshineproject.plugin.svn.commands
{
	import com.moonshineproject.plugin.core.ExternalCommandBase;
	import com.moonshineproject.plugin.svn.view.ServerCertificateDialog;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.utils.IDataOutput;
	
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;
	
	public class SVNCommandBase extends ExternalCommandBase
	{
		override public function get name():String { return "Subversion Plugin"; }
		
		public function SVNCommandBase(executable:File, root:File)
		{
			super(executable, root);
		}
		
		// Only allow one operation at a time
		protected var runningForFile:File;
		
		/*
			Handle SVN asking about Server Certificate approval/rejection 
		*/
		protected function serverCertificatePrompt(data:String):Boolean
		{
			// Server certification needs to be accepted to continue
			if (data.indexOf("Error validating server certificate for") > -1)
			{
				// Strip stuff we don't want
				data = data.replace("(R)eject, accept (t)emporarily or accept (p)ermanently?", "");
				
				var d:ServerCertificateDialog = new ServerCertificateDialog();
				d.prompt = data;
				d.addEventListener(ServerCertificateDialog.EVENT_ACCEPT_PERM, acceptPerm);
				d.addEventListener(ServerCertificateDialog.EVENT_ACCEPT_TEMP, acceptTemp);
				d.addEventListener(ServerCertificateDialog.EVENT_CANCEL, dontAccept);
				
				PopUpManager.addPopUp(d, Moonshine(FlexGlobals.topLevelApplication));
				PopUpManager.centerPopUp(d);
				
				return true;	
			}
			
			return false;
		}
		
		// (R)eject, accept (t)emporarily or accept (p)ermanently?
		protected function acceptPerm(event:Event):void
		{
			var input:IDataOutput = customProcess.standardInput;
			input.writeUTFBytes("p\n");
			removeCertDialog(event);
		}
		
		protected function acceptTemp(event:Event):void
		{
			var input:IDataOutput = customProcess.standardInput;
			input.writeUTFBytes("t\n");
			removeCertDialog(event);
		}
		
		protected function dontAccept(event:Event):void
		{
			var input:IDataOutput = customProcess.standardInput;
			input.writeUTFBytes("r\n");
			removeCertDialog(event);
		}
		
		protected function removeCertDialog(event:Event):void
		{
			var d:ServerCertificateDialog = ServerCertificateDialog(event.target);
			PopUpManager.removePopUp(d);
			
			d.removeEventListener(ServerCertificateDialog.EVENT_ACCEPT_PERM, acceptPerm);
			d.removeEventListener(ServerCertificateDialog.EVENT_ACCEPT_TEMP, acceptTemp);
			d.removeEventListener(ServerCertificateDialog.EVENT_CANCEL, dontAccept);
		}
		
	}
}